package com.wushijia.highconcurrency;

public class BadLockOnInteger implements Runnable {

  public static Integer i = 0;

  static BadLockOnInteger badLockOnInteger = new BadLockOnInteger();

  @Override
  public void run() {

    for (int j = 0; j < 10000; j++) {
      //如果此处锁对象为i,则会有问题
      synchronized (badLockOnInteger) {
        i++;
      }
    }
  }

  public static void main(String[] args) throws InterruptedException {
    Thread t1 = new Thread(badLockOnInteger);
    Thread t2 = new Thread(badLockOnInteger);

    t1.start();
    t2.start();
    t1.join();
    t2.join();

    System.out.println(i);
  }
}
